package com.czy.owner.utils;

import android.app.Activity;
import android.content.Context;

import com.czy.owner.global.Constants;
import com.czy.owner.entity.AdvertisementEntity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by King on 2017-03-07
 */

public class AdvertisementManager {
    private final String TAG = getClass().getSimpleName();
    private static AdvertisementManager mInstance = null;
    private List<AdvertisementEntity> advertisementData = new ArrayList<>();
    public static final String AD_TYPE_START_PAGE = "startPage";
    public static final String AD_TYPE_GUIDE_PAGE = "guidePage";
    public static final String AD_TYPE_ADV_PAGE = "advPage";
    private final String AD_TYPE_ALL_PAGE = "all";
    public static synchronized AdvertisementManager getInstance() {
        if (mInstance == null) {
            mInstance = new AdvertisementManager();
        }
        return mInstance;
    }

    public void getAdvertisementConfig(final Context mContext) {
        RequestParams params = new RequestParams(Constants.PUB_APP_ADS);
        /*
        appType app终端类型，必填项，storeApp 门店APP,ownerApp 车主APP
        advType 广告类型,必填项，guidePage 引导页,startPage 启动页面,advPage 广告页 ,all 所有
         */
        params.addBodyParameter("appType", "ownerApp");
        params.addBodyParameter("advType", AD_TYPE_ALL_PAGE);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                MyLog.e("yang","广告页onSuccess=="+result);
                String data = PhoneUtils.checkResponseFlag((Activity) mContext, result);
                if (data != null && !data.equals("[]")) {
                    SharedPreferencesUtils.setParam(mContext, Constants.SHARED_PREFERENCES_KEY_ADVERTISEMENT, data);
                    advertisementData = new Gson().fromJson(data, new TypeToken<List<AdvertisementEntity>>() {
                    }.getType());
                }else{
                    SharedPreferencesUtils.remove(Constants.SHARED_PREFERENCES_KEY_ADVERTISEMENT);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ex.printStackTrace();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    public AdvertisementEntity getAdvertisementInformation(final Context mContext,String advType) {
        if (advertisementData==null || advertisementData.size() == 0) {
            String strData = (String) SharedPreferencesUtils.getParam(mContext, Constants.SHARED_PREFERENCES_KEY_ADVERTISEMENT, "");
            advertisementData = new Gson().fromJson(strData, new TypeToken<List<AdvertisementEntity>>() {
            }.getType());
        }
        if (advertisementData == null || advertisementData.size() == 0) {
            return null;
        } else {
            for(int i = 0 ;i < advertisementData.size();i++){
                //x.image().loadDrawable(advertisementData.get(i).getPicUrl(), null, null);
                if(advertisementData.get(i).getAdvType().equals(advType)){
                    if (advertisementData.get(i).getStopTime() == 0 || (advertisementData.get(i).getStartTime() < System.currentTimeMillis() && advertisementData.get(i).getStopTime() > System.currentTimeMillis())) {
                        return advertisementData.get(i);
                    } else {
                        return null;
                    }
                }
            }
            return null;
        }
    }

}
